package com.gimi.test_jpa.com.gimi.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 *
 * consumeMode - 消费模式
 *  默认值：ConsumeMode.CONCURRENTLY并行接受
 *  ConsumeMode.ORDERLY每个队列使用一个线程按顺序接收
 *
 * messageModel - 消息模式
 *  默认值：MessageModel.CLUSTERING集群模式
 *  MessageModel.BROADCASTING广播模式
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "queue_test_topic",
        selectorExpression="*",
        consumerGroup = "testJpa_group",
        consumeMode = ConsumeMode.ORDERLY)
public class RocketMQMsgListener implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt message) {
        byte[] body = message.getBody();
        String msg = new String(body);
        String topic = message.getTopic();
        String tags = message.getTags();
        String keys = message.getKeys();
        log.info("接收到消息,topic:{},msg:{},tag:{},key:{}", topic,msg,tags,keys);
    }

}
